package com.aixin.csopservice.utils;

import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class Util {

    public static String getUUID(){
       return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }

    public static String getNowDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        return dateFormat.format(date);

    }

    public static String dateFormat(Date date) {
        SimpleDateFormat  dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        return dateFormat.format(date);
    }

    public  static <T,S extends T> T getFatherClass(S classObject)  throws Exception{
        Class objectClass= classObject.getClass();
        Class father=objectClass.getSuperclass();
        Field[] fields = objectClass.getDeclaredFields();
        for (Field field : fields) {
           System.out.println(field.getGenericType());
           System.out.println(father.getName());

            if (field.getGenericType().toString().equals(father.getName())){
                Method m=father.getMethod(getMethodName(field.getName()));
                return   (T)m.invoke(objectClass);
            }
        }
       return  null;
    }

    private static String getMethodName(String fildeName) {
                 byte[] items = fildeName.getBytes();
               items[0] = (byte) ((char) items[0] - 'a' + 'A');
               return new String(items);
             }


    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            // 设置lenient为false.
            // 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }

}
